/****************************************************************
 ** Product  :   HP Subscriber Network Application Policy
 ** Module   :   gr-manager
 ** Date: Apr 10, 2015               
 ** Author: Chen Zhenhua
 ** (C) Copyright 2015, Hewlett-Packard Company, All Rights Reserved.
 ** This software is the proprietary information of HP, Inc.  
 ** Use is subject to license terms.
 *****************************************************************
 ** Description:   
 **      It is a Atomic action to check session server business service status 
 **    
 ****************************************************************/
package com.hp.snap.gr.action.impl;

import com.hp.snap.gr.exception.ActionException;
import com.hp.snap.gr.exception.BusinessException;
import com.hp.snap.gr.model.BizServiceStatus;
import com.hp.snap.gr.model.SessionServerStatus;
import com.hp.snap.gr.model.jaxb.ActionType;
import com.hp.snap.gr.model.jaxb.GeneralActionType;
import com.hp.snap.gr.model.jaxb.ParameterType;
import com.hp.snap.gr.utils.Constants;
import com.hp.snap.gr.utils.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

/**
 * It is a Atomic action to check session server business service status.
 * The action need to configure a "Enable_Status" parameter as checking condition, the "Enable_Status" should be configured to "Starting","Standby"or "Running" value.
 * Get the session server status map from action plan output, in the previous action, the session server status map can be save the action plan output.
 * If the session server business status does not match the Enable_status value, it will result in a  BusinessException and interrupt the execution.
 * 
 * <P>
 * Version: 3.3 <br>
 * Author: Chen Zhenhua
 * Date: Apr 10, 2015
 * 
 * </P>
 *
 **/
public class CheckSessionServerBizServiceEnableActionHandler extends
        AbstractActionHandler {

    private static Logger logger = LoggerFactory.getLogger(CheckSessionServerBizServiceEnableActionHandler.class);

    @Override
    public void execute(ActionType action,
                        Map<String, Object> actionPlanInputs,
                        Map<String, Object> actionPlanOutputs) throws ActionException {
        

        GeneralActionType actionType = (GeneralActionType) action;
        List<ParameterType> parameterTypes = actionType.getParameter();
        Map<String, String> parameterMap = Utils.contructParamMap(parameterTypes);

        String status = parameterMap.get("Enable_Status");
        BizServiceStatus bizService = BizServiceStatus.valueOf(status);

        Map<String, SessionServerStatus> ssStatusMap = (Map<String, SessionServerStatus>) actionPlanOutputs.get(Constants.STATUS_SESSION_SERVER);
        Map<String, Integer> ssTypeStartCounter = Utils.getSSTypeCounter(ssStatusMap.keySet());
        logger.info("SessionServerStatus is {}", ssStatusMap);
        logger.info("BizServiceStatus=={}", bizService);
        boolean ssStatusIsOK = true;
        if (ssStatusMap.size() == 0 && bizService == BizServiceStatus.Standby)
            ssStatusIsOK = true;
        else if (ssStatusMap.size() == 0 && bizService != BizServiceStatus.Standby)
            ssStatusIsOK = false;
        for (Map.Entry<String, SessionServerStatus> entry : ssStatusMap.entrySet()) {
            SessionServerStatus masterStatus = entry.getValue();
            if (logger.isDebugEnabled()) {
                logger.debug(" SS name: {}, State:{}, Status:{}, BizServiceStatus:{}", new Object[]{masterStatus.getName(), masterStatus.getProcessStatus(), masterStatus.getKernelStatus(), masterStatus.getBizServiceStatus()});
            }

            if (bizService != masterStatus.getBizServiceStatus())
                ssStatusIsOK &= false;

        }

        if (!ssStatusIsOK) {
            throw new BusinessException("Some session server Biz service status are not " + status + "!!");
        }

    }
}
